<?php
    
    
	require_once '../includes.php';  
    
    $action = "db:migrate";
    
    echo "los geht's:<pre>";
    
    // do reflection
    if($action == "db:migrate") {
        // load and reflect models
        $dir = getcwd();
        chdir("../");
        
        // Include all pimp classes
        $handle = opendir('app/models');
        while (false !== ($file = readdir ($handle))) {
            if ($file != "." && $file != ".." && strpos($file, ".php") != false && strpos($file, "#") === false && strpos($file, ".swp") === false  ) { 
                include_once "models/" . $file;
                $class_name = basename ($file ,".php");
                
                // instanciate class
                $class = new $class_name;
                if(is_subclass_of($class, "AwesomeRecord")) {
                    // try to create base table
                    $table_name = Inflector::tableize($class_name);
                    Database::execute("CREATE TABLE IF NOT EXISTS `{$table_name}` (`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY);");
                    
                    // get fields
                    $class_fields = $class::$fields;
                    $table_fields_pdo = Database::query("SHOW FIELDS FROM `{$table_name}`;");
                    $table_fields = array();
                    foreach($table_fields_pdo->fetchAll(PDO::FETCH_CLASS) as $field) {
                        $table_fields[$field->Field] = $field;
                    }
                    
                    foreach($class_fields as $field => $params) {
                        if(!isset($table_fields[$field])) {
                            echo $params;
                            
                            // string
                            if(preg_match('!string\[(?P<min>\d+)..(?P<max>\d+)\]!i', $params, $matches)) {
                                Database::execute("ALTER TABLE `{$table_name}` ADD `{$field}` VARCHAR({$matches['max']}) NULL;");
                            }                            
                            
                            // text
                            if(preg_match('!text!i', $params, $matches)) {
                                Database::execute("ALTER TABLE `{$table_name}` ADD `{$field}` TEXT NULL;");
                            }
                            
                            // integer
                            if(preg_match('!integer!i', $params, $matches)) {
                                Database::execute("ALTER TABLE `{$table_name}` ADD `{$field}` INT NULL;");
                            }
                            
                            // integer
                            if(preg_match('!integer!i', $params, $matches)) {
                                Database::execute("ALTER TABLE `{$table_name}` ADD `{$field}` INT NULL;");
                            }
                            
                            // @TODO float
                            // @TODO decimal
                            
                            // datetime
                            if(preg_match('!datetime!i', $params, $matches)) {
                                Database::execute("ALTER TABLE `{$table_name}` ADD `{$field}` DATETIME NULL;");
                            }
                            
                            // timestamp
                            if(preg_match('!timestamp!i', $params, $matches)) {
                                Database::execute("ALTER TABLE `{$table_name}` ADD `{$field}` TIMESTAMP NULL;");
                            }
                            
                            // date
                            if(preg_match('!date!i', $params, $matches)) {
                                Database::execute("ALTER TABLE `{$table_name}` ADD `{$field}` DATE NULL;");
                            }
                            
                            // time
                            if(preg_match('!time!i', $params, $matches)) {
                                Database::execute("ALTER TABLE `{$table_name}` ADD `{$field}` TIME NULL;");
                            }
                            
                            // binary
                            if(preg_match('!binary!i', $params, $matches)) {
                                Database::execute("ALTER TABLE `{$table_name}` ADD `{$field}` BINARY NULL;");
                            }
                            
                            // boolean
                            if(preg_match('!boolean!i', $params, $matches)) {
                                Database::execute("ALTER TABLE `{$table_name}` ADD `{$field}` BOOL NULL;");
                            }
                            
                            echo "generate {$table_name}.{$field}...\n";
                        }
                    }
                }
            }
        }
        closedir($handle);
        chdir($dir);
    }
?>